using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XTYAPI.PLC
{
  public  class 滑动平均法
    {
        public 滑动平均法(int count)
        {
            Data = new double[count];
        }
        double iN;
        public double SUM { get;private set; }
        public double IN { get { return iN; } set { iN = value; Cal(); } }
        public double Result { get; set; }
        public int NowCount { get;private set; }
        public double[] Data { get;private set; }
        public double Cal()
        {
            if (NowCount< Data.Length)//队列未达到算法
            {
                Data[NowCount] = IN;//填充数组
                SUM += IN;//求和
                if (SUM != 0) Result = SUM / (NowCount+1);
                NowCount++;//队列填充计数
            }
            else//达到算法
            {
                SUM = SUM - Data[0];//去掉旧数据
                for (int i = 0; i < Data.Length-1; i++)//数据向前移动
                {
                    Data[i] = Data[i + 1];
                }
                Data[Data.Length - 1] = IN;
                SUM += Data[Data.Length - 1];//加上最新数据
                Result = SUM / Data.Length;//计算平均值
            }
            return Result;
        }
    }
}
